/*
 * CONFIDENTIAL AND PROPRIETARY. (c) 2007 Revolution Health Group LLC. All rights reserved. 
 * This source code may not be disclosed to others, used or reproduced without the written permission of Revolution Health Group.
 */
package org.javason.jsonrpc;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.ArrayList;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.commons.httpclient.methods.PostMethod;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.test.JSONAssert;
import net.sf.json.util.JSONUtils;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

import org.javason.json.BeanToJsonSerializer;

import junit.framework.TestCase;

public class TestArrayAndEnum extends TestCase{

	public void testArrays() throws JSONException {
	
		System.out.println("testArrays()");
		
		try {
			RelationshipTypeList rtl = RelationshipTypeList.CHILD;
			ArrayOfRelationshipTypeList arryRel = new ArrayOfRelationshipTypeList();
			arryRel.getRelationshipTypeList().add(rtl);
			for (RelationshipTypeList type : RelationshipTypeList.class.getEnumConstants()) {
				System.out.println("type = " + type);
			}
			String serialized = BeanToJsonSerializer.serialize(arryRel).toString();
			System.out .println("Inside testArrays method " + serialized);
			assertEquals("{\"relationshipTypeList\":[\""+ RelationshipTypeList.CHILD.value() +"\"]}", serialized);
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		
	public void testEnum() {
		System.out.println("RUNNING testEnum()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		try {
			JSONArray param1 = new JSONArray();
			
			RelationshipTypeList rtl = RelationshipTypeList.CHILD;
			ArrayOfRelationshipTypeList arryRel = new ArrayOfRelationshipTypeList();
			arryRel.getRelationshipTypeList().add(rtl);
			param1.add(BeanToJsonSerializer.serialize(arryRel));
			
			System.out.println("params = " + param1.toString(3));
			
			JSONObject blah = JsonRpcRequestFactory.createJsonRpcRequest(47, "doSomething", param1);
			request.setContent(blah.toString().getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new ArrayWithEnum());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println("Status: " + response.getStatus());
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertEquals(47, responseObj.getInt("id"));
			
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}
	
		
		public void testArrays2() throws JSONException {
			/*
			System.out.println("testArrays2()");
			
			try {
				JSONObject quoteConfig = new JSONObject();
				JSONArray quoteConfigs = new JSONArray();
				
				WsfContactInformation wsfContactInfo = new WsfContactInformation();
				wsfContactInfo.setEmailAddress("hpandalai@yahoo.com");
				wsfContactInfo.setPreferredMethodOfContact(MethodOfContactList.EMAIL);

				WsfPerson wsfPerson = new WsfPerson();
				wsfPerson.setFirstName("Hari");
				wsfPerson.setLastName("Pandalai");
				wsfPerson.setWsfContactInformation(wsfContactInfo);

				
				WsfCustomer wsfCustomer = new WsfCustomer();
				wsfCustomer.setWsfPerson(wsfPerson);
				wsfCustomer.setDateOfBirth(DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar(1975, Calendar.DECEMBER,Calendar.DAY_OF_YEAR)));
				wsfCustomer.setCustomerId(0L);

				ArrayOfRelationshipTypeList arryRel = new ArrayOfRelationshipTypeList();
				arryRel.getRelationshipTypeList().add(RelationshipTypeList.CHILD);
				wsfCustomer.setRelationshipType(arryRel);
				wsfCustomer.setAssociatedCampaignId(2);
				wsfCustomer.setGenderCode(GenderCodeList.M);
				wsfCustomer.setUsesTobacco(Boolean.FALSE);
				wsfCustomer.setIsStudent(Boolean.FALSE);
				wsfCustomer.setLastVisitedOnDate(null);

				
				WsfQuoteConfiguration wsfQuoteConfig = new WsfQuoteConfiguration();
				wsfQuoteConfig.setCampaignId(2);
				wsfQuoteConfig.setRequestedPlanType(PlanTypeList.IMM);
				wsfQuoteConfig.setQuoteConfigurationId(0L);
				wsfQuoteConfig.setZipCode("20841");
				wsfQuoteConfig.setCountyName("Montgomery");
				wsfQuoteConfig.setEffectiveDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar(2007, Calendar.JANUARY,Calendar.DAY_OF_WEEK)));

				ArrayOfWsfCustomer arryWsfCustomers = new ArrayOfWsfCustomer();
				arryWsfCustomers.getWsfCustomer().add(wsfCustomer);
				
				wsfQuoteConfig.setWsfCustomers(arryWsfCustomers);

				quoteConfigs.put(0,BeanToJsonSerializer.serializeToJsonObject(wsfQuoteConfig));
				System.out .println("Inside testArrays2 method " +quoteConfigs);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
*/
	}
}
